//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using System.Web.Compilation;
using Tathata.Common.BLL.EntityCollections;
using System.IO;
using System.Xml;
using System.Web;
using Tathata.Common.Utils;

namespace Tathata.Common.ExportImport
{
    /// <summary>
    /// Export manager
    /// </summary>
    public class ExportManager
    {
        #region Methods

        /// <summary>
        /// Export customer list to xml
        /// </summary>
        /// <returns>Result in XML format</returns>
        public static string ExportCustomersToXML()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter stringWriter = new StringWriter(sb);
            XmlWriter xmlWriter = new XmlTextWriter(stringWriter);
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("Customers");
            CustomerCollection customers = CustomerManager.GetAllCustomers();

            foreach (Customer customer in customers)
            {
                xmlWriter.WriteStartElement("Customer");
                xmlWriter.WriteElementString("CustomerID", null, customer.CustomerID.ToString());
                xmlWriter.WriteElementString("CustomerGUID", null, customer.CustomerGUID.ToString());
                xmlWriter.WriteElementString("Email", null, customer.Email);
                xmlWriter.WriteElementString("PasswordHash", null, customer.PasswordHash);
                xmlWriter.WriteElementString("SaltKey", null, customer.SaltKey);
                xmlWriter.WriteElementString("AffiliateID", null, customer.AffiliateID.ToString());
                xmlWriter.WriteElementString("IsAdmin", null, customer.IsAdmin.ToString());
                xmlWriter.WriteElementString("Active", null, customer.Active.ToString());
                xmlWriter.WriteElementString("Deleted", null, customer.Deleted.ToString());
                xmlWriter.WriteElementString("RegistrationDate", null, customer.RegistrationDate.ToString());

                CustomerAttributeCollection customerAttributes = customer.CustomerAttributes;
                CustomerAttribute genderAttr = customerAttributes.FindAttribute("Gender", customer.CustomerID);
                if (genderAttr != null)
                    xmlWriter.WriteElementString("Gender", null, genderAttr.Value);
                CustomerAttribute firstNameAttr = customerAttributes.FindAttribute("FirstName", customer.CustomerID);
                if (firstNameAttr != null)
                    xmlWriter.WriteElementString("FirstName", null, firstNameAttr.Value);
                CustomerAttribute lastNameAttr = customerAttributes.FindAttribute("LastName", customer.CustomerID);
                if (lastNameAttr != null)
                    xmlWriter.WriteElementString("LastName", null, lastNameAttr.Value);
                CustomerAttribute dateOfBirthAttr = customerAttributes.FindAttribute("DateOfBirth", customer.CustomerID);
                if (dateOfBirthAttr != null)
                    xmlWriter.WriteElementString("DateOfBirth", null, dateOfBirthAttr.Value);
                CustomerAttribute companyAttr = customerAttributes.FindAttribute("Company", customer.CustomerID);
                if (companyAttr != null)
                    xmlWriter.WriteElementString("Company", null, companyAttr.Value);
                CustomerAttribute streetAddressAttr = customerAttributes.FindAttribute("StreetAddress", customer.CustomerID);
                if (streetAddressAttr != null)
                    xmlWriter.WriteElementString("StreetAddress", null, streetAddressAttr.Value);
                CustomerAttribute streetAddress2Attr = customerAttributes.FindAttribute("StreetAddress2", customer.CustomerID);
                if (streetAddress2Attr != null)
                    xmlWriter.WriteElementString("StreetAddress2", null, streetAddress2Attr.Value);
                CustomerAttribute zipPostalCodeAttr = customerAttributes.FindAttribute("ZipPostalCode", customer.CustomerID);
                if (zipPostalCodeAttr != null)
                    xmlWriter.WriteElementString("ZipPostalCode", null, zipPostalCodeAttr.Value);
                CustomerAttribute cityAttr = customerAttributes.FindAttribute("City", customer.CustomerID);
                if (cityAttr != null)
                    xmlWriter.WriteElementString("City", null, cityAttr.Value);
                CustomerAttribute phoneNumberAttr = customerAttributes.FindAttribute("PhoneNumber", customer.CustomerID);
                if (phoneNumberAttr != null)
                    xmlWriter.WriteElementString("PhoneNumber", null, phoneNumberAttr.Value);
                CustomerAttribute faxNumberAttr = customerAttributes.FindAttribute("FaxNumber", customer.CustomerID);
                if (faxNumberAttr != null)
                    xmlWriter.WriteElementString("FaxNumber", null, faxNumberAttr.Value);
                CustomerAttribute countryIDAttr = customerAttributes.FindAttribute("CountryID", customer.CustomerID);
                if (countryIDAttr != null)
                {
                    xmlWriter.WriteElementString("CountryID", null, countryIDAttr.Value);
                    Country country = CountryManager.GetByCountryID(int.Parse(countryIDAttr.Value));
                    xmlWriter.WriteElementString("Country", null, (country == null) ? string.Empty : country.Name);
                }
                CustomerAttribute stateProvinceIDAttr = customerAttributes.FindAttribute("StateProvinceID", customer.CustomerID);
                if (stateProvinceIDAttr != null)
                {
                    xmlWriter.WriteElementString("StateProvinceID", null, stateProvinceIDAttr.Value);
                    StateProvince stateProvince = StateProvinceManager.GetByStateProvinceID(int.Parse(stateProvinceIDAttr.Value));
                    xmlWriter.WriteElementString("StateProvince", null, (stateProvince == null) ? string.Empty : stateProvince.Name);
                }

               

               
                xmlWriter.WriteEndElement();
            }

            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
            return stringWriter.ToString();
        }

        /// <summary>
        /// Export manufacturer list to xml
        /// </summary>
        /// <returns>Result in XML format</returns>
        public static string ExportManufacturersToXML()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter stringWriter = new StringWriter(sb);
            XmlWriter xmlWriter = new XmlTextWriter(stringWriter);
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("Manufacturers");
            ManufacturerCollection manufacturers = ManufacturerManager.GetAllManufacturers();

            foreach (Manufacturer manufacturer in manufacturers)
            {
                xmlWriter.WriteStartElement("Manufacturer");
                xmlWriter.WriteElementString("ManufacturerID", null, manufacturer.ManufacturerID.ToString());
                xmlWriter.WriteElementString("Name", null, manufacturer.Name);
                xmlWriter.WriteElementString("Description", null, manufacturer.Description);
                xmlWriter.WriteElementString("TemplateID", null, manufacturer.TemplateID.ToString());
                xmlWriter.WriteElementString("MetaKeywords", null, manufacturer.MetaKeywords);
                xmlWriter.WriteElementString("MetaDescription", null, manufacturer.MetaDescription);
                xmlWriter.WriteElementString("MetaTitle", null, manufacturer.MetaTitle);
                xmlWriter.WriteElementString("SEName", null, manufacturer.SEName);
                xmlWriter.WriteElementString("PictureID", null, manufacturer.PictureID.ToString());
                xmlWriter.WriteElementString("Published", null, manufacturer.Published.ToString());
                xmlWriter.WriteElementString("Deleted", null, manufacturer.Deleted.ToString());
                xmlWriter.WriteElementString("DisplayOrder", null, manufacturer.DisplayOrder.ToString());
                xmlWriter.WriteElementString("CreatedOn", null, manufacturer.CreatedOn.ToString());
                xmlWriter.WriteElementString("UpdatedOn", null, manufacturer.UpdatedOn.ToString());
                
                xmlWriter.WriteStartElement("Products");
                ProductManufacturerCollection productManufacturers = manufacturer.ProductManufacturers;
                foreach (ProductManufacturer productManufacturer in productManufacturers)
                {
                    Product product = productManufacturer.Product;
                    if (product != null)
                    {
                        xmlWriter.WriteStartElement("ProductManufacturer");
                        xmlWriter.WriteElementString("ProductManufacturerID", null, productManufacturer.ProductManufacturerID.ToString());
                        xmlWriter.WriteElementString("ProductID", null, productManufacturer.ProductID.ToString());
                        xmlWriter.WriteElementString("IsFeaturedProduct", null, productManufacturer.IsFeaturedProduct.ToString());
                        xmlWriter.WriteElementString("DisplayOrder", null, productManufacturer.DisplayOrder.ToString());
                        xmlWriter.WriteEndElement();
                    }
                }
                xmlWriter.WriteEndElement();

                xmlWriter.WriteEndElement();
            }

            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
            return stringWriter.ToString();
        }

        /// <summary>
        /// Export category list to xml
        /// </summary>
        /// <returns>Result in XML format</returns>
        public static string ExportCategoriesToXML()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter stringWriter = new StringWriter(sb);
            XmlWriter xmlWriter = new XmlTextWriter(stringWriter);
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("Categories");
            WriteCategories(xmlWriter, 0);
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
            return stringWriter.ToString();
        }
        
        /// <summary>
        /// Export product list to xml
        /// </summary>
        /// <returns>Result in XML format</returns>
        public static string ExportProductsToXML()
        {
            StringBuilder sb = new StringBuilder();
            StringWriter stringWriter = new StringWriter(sb);
            XmlWriter xmlWriter = new XmlTextWriter(stringWriter);
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("Products");
            ProductCollection products = ProductManager.GetAllProducts();

            foreach (Product product in products)
            {
                xmlWriter.WriteStartElement("Product");
                xmlWriter.WriteElementString("ProductID", null, product.ProductID.ToString());
                xmlWriter.WriteElementString("Name", null, product.Name);
                xmlWriter.WriteElementString("ShortDescription", null, product.ShortDescription);
                xmlWriter.WriteElementString("FullDescription", null, product.FullDescription);
                xmlWriter.WriteElementString("AdminComment", null, product.AdminComment);
                xmlWriter.WriteElementString("ProductTypeID", null, product.ProductTypeID.ToString());
                xmlWriter.WriteElementString("TemplateID", null, product.TemplateID.ToString());
                xmlWriter.WriteElementString("MetaKeywords", null, product.MetaKeywords);
                xmlWriter.WriteElementString("MetaDescription", null, product.MetaDescription);
                xmlWriter.WriteElementString("MetaTitle", null, product.MetaTitle);
                xmlWriter.WriteElementString("SEName", null, product.SEName);
                xmlWriter.WriteElementString("AllowCustomerReviews", null, product.AllowCustomerReviews.ToString());
                xmlWriter.WriteElementString("RatingSum", null, product.RatingSum.ToString());
                xmlWriter.WriteElementString("TotalRatingVotes", null, product.TotalRatingVotes.ToString());
                xmlWriter.WriteElementString("Published", null, product.Published.ToString());
                xmlWriter.WriteElementString("Deleted", null, product.Deleted.ToString());
                xmlWriter.WriteElementString("CreatedOn", null, product.CreatedOn.ToString());
                xmlWriter.WriteElementString("UpdatedOn", null, product.UpdatedOn.ToString());

                xmlWriter.WriteStartElement("ProductVariants");
                ProductVariantCollection productVariants = product.ProductVariants;
                foreach (ProductVariant productVariant in productVariants)
                {
                    xmlWriter.WriteStartElement("ProductVariant");
                    xmlWriter.WriteElementString("ProductVariantID", null, productVariant.ProductVariantID.ToString());
                    xmlWriter.WriteElementString("ProductID", null, productVariant.ProductID.ToString());
                    xmlWriter.WriteElementString("Name", null, productVariant.Name);
                    xmlWriter.WriteElementString("SKU", null, productVariant.SKU);
                    xmlWriter.WriteElementString("Description", null, productVariant.Description);
                    xmlWriter.WriteElementString("AdminComment", null, productVariant.AdminComment);
                    xmlWriter.WriteElementString("ManufacturerPartNumber", null, productVariant.ManufacturerPartNumber);
                    xmlWriter.WriteElementString("IsDownload", null, productVariant.IsDownload.ToString());
                    xmlWriter.WriteElementString("DownloadID", null, productVariant.DownloadID.ToString());
                    xmlWriter.WriteElementString("IsShipEnabled", null, productVariant.IsShipEnabled.ToString());
                    xmlWriter.WriteElementString("IsFreeShipping", null, productVariant.IsFreeShipping.ToString());
                    xmlWriter.WriteElementString("TaxCategoryID", null, productVariant.TaxCategoryID.ToString());
                    xmlWriter.WriteElementString("StockQuantity", null, productVariant.StockQuantity.ToString());
                    xmlWriter.WriteElementString("WarehouseId", null, productVariant.WarehouseId.ToString());
                    xmlWriter.WriteElementString("DisableBuyButton", null, productVariant.DisableBuyButton.ToString());
                    xmlWriter.WriteElementString("RequiresTextOption", null, productVariant.RequiresTextOption.ToString());
                    xmlWriter.WriteElementString("TextOptionPrompt", null, productVariant.TextOptionPrompt);
                    xmlWriter.WriteElementString("RequiresTextOption", null, productVariant.RequiresTextOption.ToString());
                    xmlWriter.WriteElementString("Price", null, productVariant.Price.ToString());
                    xmlWriter.WriteElementString("OldPrice", null, productVariant.OldPrice.ToString());
                    xmlWriter.WriteElementString("Weight", null, productVariant.Weight.ToString());
                    xmlWriter.WriteElementString("Length", null, productVariant.Length.ToString());
                    xmlWriter.WriteElementString("Width", null, productVariant.Width.ToString());
                    xmlWriter.WriteElementString("Height", null, productVariant.Height.ToString());
                    xmlWriter.WriteElementString("PictureID", null, productVariant.PictureID.ToString());
                    xmlWriter.WriteElementString("Published", null, productVariant.Published.ToString());
                    xmlWriter.WriteElementString("Deleted", null, productVariant.Deleted.ToString());
                    xmlWriter.WriteElementString("DisplayOrder", null, productVariant.DisplayOrder.ToString());
                    xmlWriter.WriteElementString("CreatedOn", null, productVariant.CreatedOn.ToString());
                    xmlWriter.WriteElementString("UpdatedOn", null, productVariant.UpdatedOn.ToString());

                    xmlWriter.WriteStartElement("ProductDiscounts");
                    DiscountCollection discounts = productVariant.Discounts;
                    foreach (Discount discount in discounts)
                    {
                        xmlWriter.WriteElementString("DiscountID", null, discount.DiscountID.ToString());
                    }
                    xmlWriter.WriteEndElement();



                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("ProductPictures");
                ProductPictureCollection productPictures = product.ProductPictures;
                foreach (ProductPicture productPicture in productPictures)
                {
                    xmlWriter.WriteStartElement("ProductPicture");
                    xmlWriter.WriteElementString("ProductPictureID", null, productPicture.ProductPictureID.ToString());
                    xmlWriter.WriteElementString("PictureID", null, productPicture.PictureID.ToString());
                    xmlWriter.WriteElementString("DisplayOrder", null, productPicture.DisplayOrder.ToString());
                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("RelatedProducts");
                RelatedProductCollection relatedProducts = product.RelatedProducts;
                foreach (RelatedProduct relatedProduct in relatedProducts)
                {
                    xmlWriter.WriteStartElement("RelatedProduct");
                    xmlWriter.WriteElementString("RelatedProductID", null, relatedProduct.RelatedProductID.ToString());
                    xmlWriter.WriteElementString("ProductID1", null, relatedProduct.ProductID1.ToString());
                    xmlWriter.WriteElementString("ProductID2", null, relatedProduct.ProductID2.ToString());
                    xmlWriter.WriteElementString("DisplayOrder", null, relatedProduct.DisplayOrder.ToString());
                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("ProductCategories");
                ProductCategoryCollection productCategories = product.ProductCategories;
                foreach (ProductCategory productCategory in productCategories)
                {
                    xmlWriter.WriteStartElement("ProductCategory");
                    xmlWriter.WriteElementString("ProductCategoryID", null, productCategory.ProductCategoryID.ToString());
                    xmlWriter.WriteElementString("CategoryID", null, productCategory.CategoryID.ToString());
                    xmlWriter.WriteElementString("IsFeaturedProduct", null, productCategory.IsFeaturedProduct.ToString());
                    xmlWriter.WriteElementString("DisplayOrder", null, productCategory.DisplayOrder.ToString());
                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("ProductManufacturers");
                ProductManufacturerCollection productManufacturers = product.ProductManufacturers;
                foreach (ProductManufacturer productManufacturer in productManufacturers)
                {
                    xmlWriter.WriteStartElement("ProductManufacturer");
                    xmlWriter.WriteElementString("ProductManufacturerID", null, productManufacturer.ProductManufacturerID.ToString());
                    xmlWriter.WriteElementString("ManufacturerID", null, productManufacturer.ManufacturerID.ToString());
                    xmlWriter.WriteElementString("IsFeaturedProduct", null, productManufacturer.IsFeaturedProduct.ToString());
                    xmlWriter.WriteElementString("DisplayOrder", null, productManufacturer.DisplayOrder.ToString());
                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("ProductSpecificationAttributes");
                ProductSpecificationAttributeCollection productSpecificationAttributes = product.ProductSpecificationAttributes;
                foreach (ProductSpecificationAttribute productSpecificationAttribute in productSpecificationAttributes)
                {
                    xmlWriter.WriteStartElement("ProductSpecificationAttribute");
                    xmlWriter.WriteElementString("ProductSpecificationAttributeID", null, productSpecificationAttribute.ProductSpecificationAttributeID.ToString());
                    xmlWriter.WriteElementString("SpecificationAttributeID", null, productSpecificationAttribute.SpecificationAttributeID.ToString());
                    xmlWriter.WriteElementString("AttributeValue", null, productSpecificationAttribute.AttributeValue);
                    xmlWriter.WriteElementString("DisplayOrder", null, productSpecificationAttribute.DisplayOrder.ToString());
                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();

                xmlWriter.WriteEndElement();
            }

            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
            return stringWriter.ToString();
        }

        /// <summary>
        /// Export order list to xml
        /// </summary>
        /// <returns>Result in XML format</returns>
      

        private static void WriteCategories(XmlWriter xmlWriter, int ParentCategoryID)
        {
            CategoryCollection categories = CategoryManager.GetAllCategories(ParentCategoryID);
            if (categories.Count > 0)
            {
                foreach (Category category in categories)
                {
                    xmlWriter.WriteStartElement("Category");
                    xmlWriter.WriteElementString("CategoryID", null, category.CategoryID.ToString());
                    xmlWriter.WriteElementString("Name", null, category.Name);
                    xmlWriter.WriteElementString("Description", null, category.Description);
                    xmlWriter.WriteElementString("TemplateID", null, category.TemplateID.ToString());
                    xmlWriter.WriteElementString("MetaKeywords", null, category.MetaKeywords);
                    xmlWriter.WriteElementString("MetaDescription", null, category.MetaDescription);
                    xmlWriter.WriteElementString("MetaTitle", null, category.MetaTitle);
                    xmlWriter.WriteElementString("SEName", null, category.SEName);
                    xmlWriter.WriteElementString("ParentCategoryID", null, category.ParentCategoryID.ToString());
                    xmlWriter.WriteElementString("PictureID", null, category.PictureID.ToString());
                    xmlWriter.WriteElementString("Published", null, category.Published.ToString());
                    xmlWriter.WriteElementString("Deleted", null, category.Deleted.ToString());
                    xmlWriter.WriteElementString("DisplayOrder", null, category.DisplayOrder.ToString());
                    xmlWriter.WriteElementString("CreatedOn", null, category.CreatedOn.ToString());
                    xmlWriter.WriteElementString("UpdatedOn", null, category.UpdatedOn.ToString());

                    xmlWriter.WriteStartElement("Products");
                    ProductCategoryCollection productCategories = category.ProductCategories;
                    foreach (ProductCategory productCategory in productCategories)
                    {
                        Product product = productCategory.Product;
                        if (product != null)
                        {
                            xmlWriter.WriteStartElement("ProductCategory");
                            xmlWriter.WriteElementString("ProductCategoryID", null, productCategory.ProductCategoryID.ToString());
                            xmlWriter.WriteElementString("ProductID", null, productCategory.ProductID.ToString());
                            xmlWriter.WriteElementString("IsFeaturedProduct", null, productCategory.IsFeaturedProduct.ToString());
                            xmlWriter.WriteElementString("DisplayOrder", null, productCategory.DisplayOrder.ToString());
                            xmlWriter.WriteEndElement();
                        }
                    }
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("SubCategories");
                    WriteCategories(xmlWriter, category.CategoryID);
                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndElement();
                }
            }
        }

        #endregion
    }
}
